import { myRequest } from "@/utils/request"
import { userManager } from "@/utils/user"
import {tokenManager} from "@/utils/token.js";

export var userServie={
    register:function(user,success){
      myRequest.request({
          url:'users',
          method:'POST',
          data:user,
          success:success
      })
    },
    login:function(user,success){
         myRequest.request({
            url:'token',
            method:'POST',
            data:user,
            success:success
         })

    },
    updateUserInfo:function(user,success){
      myRequest.formRequest({
        url:'users/'+userManager.getUserId(),
        method:'PUT',
        data:user,
        success:success
      })
    },
    emailVerificationCode:function(email,success){
      myRequest.request({
        url:'users/email/verification_code/'+email,
        method:'GET',
        data:null,
        success:success
      })
    },
    /**
    * 激活当前登录用户为审核员身份
    * @param {string} auditorPassword - 审核员密码
    * @returns {Promise<any>}
    */
    activateAuditor: function(auditorPassword) {
        return new Promise((resolve, reject) => {
            myRequest.request({
                url: 'users/activate-auditor', // 对应 UserController.java [cite: 7]
                method: 'POST',
                data: { auditorPassword: auditorPassword },
                success: (res) => {
                    resolve(res);
                },
                error: (err) => {
                    reject(err);
                }
            });
        });
    },
    getSignInfo:function(user_id,success){
      myRequest.request({
        url:'users/'+user_id+'/signin',
        method:'GET',
        data:null,
        success:success
      })
    },
    signIn:function(user_id,success){
      myRequest.request({
        url:'users/'+user_id+'/signin',
        method:'POST',
        data:null,
        success:success
      })
    },
    getUserById(userId) {
        return new Promise((resolve, reject) => {
            myRequest.request({
                url: `users/${userId}`,
                method: 'GET',
                data:null,
                success: (response) => {
                    resolve(response);
                },
                error: (err) => {
                    reject(err);
                }
            });
        })
    },
    getMyInfo(callback) {
        myRequest.request({
            url: 'users/me', // 后端新增的接口路径
            method: 'GET',
            header: {
                // 【重要】在请求头中带上token
                'token': tokenManager.getToken()
            },
            success: (res) => {
                callback(res);
            },
            fail: (err) => {
                console.error('获取用户信息失败:', err);
                // 可以在这里也执行回调，并传入错误信息
                callback({ code: -1, msg: '请求失败' });
            }
        });
    },
    /**
     * 【新增方法】获取当前用户信息，返回 Promise 以便使用 async/await
     * @returns {Promise<any>} 包含用户信息的 Result 对象
     */
    getMyInfoPromise: function() { // 新增的方法
        return new Promise((resolve, reject) => {
            myRequest.request({
                url: 'users/me', // 对应 UserController.java 中的 /users/me 接口
                method: 'GET',
                header: {
                    'token': tokenManager.getToken()
                },
                success: (res) => {
                    resolve(res);
                },
                error: (err) => { // myRequest.request 的 error 回调
                    console.error('获取用户信息失败:', err);
                    reject(err); // 失败时 reject 错误，这样 await 才能捕获
                }
            });
        });
    },
    forgotPassword: function(email, success) {
    myRequest.request({
        url: 'users/forgot-password',
        method: 'POST',
        data: { email: email },
        success: success
    });
    },

    resetPassword: function(token, newPassword, success) {
    myRequest.request({
        url: 'users/reset-password',
        method: 'POST',
        data: {
            token: token,
            newPassword: newPassword
        },
        success: success
    })},

    getFrameItemId(user_id,success){
            myRequest.request({
                url:'users/'+user_id+'/frame',
                method:'GET',
                data:null,
                success:success
            })
        }

        
    
    

}